package com.cicc.wm.lock;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import io.netty.util.TimerTask;
import io.netty.util.concurrent.DefaultThreadFactory;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * jdbc锁调度服务
 */
@Slf4j
public final class WheelTimerService {
    private static final HashedWheelTimer HASHED_WHEEL_TIMER =
            new HashedWheelTimer(new DefaultThreadFactory("jdbc-timer"), 100,
                TimeUnit.MILLISECONDS, 1024, false);

    private WheelTimerService() {
    }

    public static Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
        try {
            return HASHED_WHEEL_TIMER.newTimeout(task, delay, unit);
        } catch (IllegalStateException exception) {
            log.error("timer is shutdown, please check the thread pool status");
            throw exception;
        }
    }
}
